package com.github.xiaolyuh.flush;

import com.github.xiaolyuh.enums.ModelEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Map;

/**
 * 本地缓存清理工厂
 *
 * @author xuwanglong
 */

@Component
public class LocalCacheFlushFactory {

    private static final String BEAN_NAME_FORMAT = "%sLocalCacheFlushManager";

    @Autowired
    private Map<String, ILocalCacheFlushManager> cacheFlushManagerConfig;

    public ILocalCacheFlushManager getFactory(ModelEnum modelEnum) {
        String beanName = String.format(BEAN_NAME_FORMAT, modelEnum.getModel());
        ILocalCacheFlushManager cacheFlushManager = cacheFlushManagerConfig.get(beanName);
        if (cacheFlushManager == null) {
            return cacheFlushManagerConfig.get(String.format(BEAN_NAME_FORMAT, "default"));
        } else {
            return cacheFlushManager;
        }
    }

}
